//
//  MySearchBarController.m
//  Ghac 4S
//
//  Created by apple on 11-11-30.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//
#import "Global.h"
#import "MySearchBarController.h"

@implementation MySearchBarController
@synthesize textfield;
@synthesize fieldBGBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
        
    }
    return self;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(void)setViewFrame:(CGRect)frame
{
    [self.view setFrame:frame];
    m_oldRect = frame;
}
-(void)keyboardWillShow:(id)sender
{
    NSNotification* notification = sender;
    NSDictionary* userinfo = notification.userInfo;
    NSValue* value = [userinfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect rect = [value CGRectValue];
    UIView* mainView = ((UIViewController*)[Global mainRootController]).view;
    rect = [mainView convertRect:rect toView:self.view.superview];
    CGRect frame = self.view.frame;

    [UIView beginAnimations:@"barAnimation" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:mainView cache:YES];
    frame.origin.y = rect.origin.y - 40;//frame.size.height;
    [self.view setFrame:frame];
    self.fieldBGBar.alpha = 1;
    [UIView commitAnimations];

}
-(void)keyboardWillHide:(id)sender
{
    UIView* mainView = ((UIViewController*)[Global mainRootController]).view;
    [UIView beginAnimations:@"barAnimation" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:mainView cache:YES];
    [self.view setFrame:m_oldRect];
    self.fieldBGBar.alpha = 0;
    [UIView commitAnimations];

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textfield resignFirstResponder];
    
    return YES;
}
#pragma mark - View lifecycle
-(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    // Do any additional setup after loading the view from its nib.
    self.fieldBGBar.alpha = 0;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.textfield = nil;
    self.fieldBGBar = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    [self viewDidUnload];
    [super dealloc];
}
@end
